﻿using AF_Application.Interface.Cache;
using AF_Application.Interface.Domain;
using AF_Application.Interface.Log;
using AF_Application.Interface.Repertory;
using AF_Application.Interface.Specifications;
using AF_Tools;
using AF_DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace AF_Application.Domain
{
    public class BaseDomain : IBaseDomain
    {
        protected ICacheProvider CacheProvider = null;
        protected ISessionCacheProvider SessionCacheProvider = null;
        protected ILogProvider LogProvider = null;
        public IDBRepertory DBRepertory { get; set; }

        public BaseDomain()
        {
        }

        public virtual void Dispose()
        {
        }


        public virtual bool Delete<T>(T entity, bool saveNow = true) where T : class, IDataModel
        {
            return DBRepertory.Delete<T>(entity, saveNow);
        }

        public virtual int Count<T>() where T : class, IDataModel
        {
            return DBRepertory.Count<T>();
        }

        public virtual int Count<T>(ISpecification<T> condition) where T : class, IDataModel
        {
            return DBRepertory.Count<T>(condition);
        }

        public virtual IQueryable<T> GetAll<T>(EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.GetAll<T>(queryOption);
        }

        public virtual IQueryable<T> GetAll<T>(Expression<Func<T, dynamic>> order, EnumSortType orderType, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.GetAll<T>(order, orderType, queryOption);
        }

        public virtual IQueryable<T> GetAll<T>(string order, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.GetAll<T>(order, queryOption);
        }

        public virtual PagedResult<T> GetAll<T>(Expression<Func<T, dynamic>> order, EnumSortType orderType, int pageIndex, int pageSize, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.GetAll<T>(order, orderType, pageIndex, pageSize, queryOption);
        }

        public virtual PagedResult<T> GetAll<T>(string order, int pageIndex, int pageSize, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.GetAll<T>(order, pageIndex, pageSize, queryOption);
        }

        public virtual T GetOne<T>(ISpecification<T> condition, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.GetOne<T>(condition, queryOption);
        }

        public virtual T GetOne<T>(ISpecification<T> condition, Expression<Func<T, dynamic>> order, EnumSortType orderType, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.GetOne<T>(condition, order, orderType, queryOption);
        }

        public virtual T GetOne<T>(ISpecification<T> condition, string order, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.GetOne<T>(condition, order, queryOption);
        }

        public virtual IQueryable<T> Find<T>(ISpecification<T> condition, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.Find<T>(condition, queryOption);
        }

        public virtual IQueryable<T> Find<T>(ISpecification<T> condition, Expression<Func<T, dynamic>> order, EnumSortType orderType, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.Find<T>(condition, order, orderType, queryOption);
        }

        public virtual IQueryable<T> Find<T>(ISpecification<T> condition, string order, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.Find<T>(condition, order, queryOption);
        }

        public virtual PagedResult<T> Find<T>(ISpecification<T> condition, Expression<Func<T, dynamic>> order, EnumSortType orderType, int pageIndex, int pageSize, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.Find<T>(condition, order, orderType, pageIndex, pageSize, queryOption);
        }

        public virtual PagedResult<T> Find<T>(ISpecification<T> condition, string order, int pageIndex, int pageSize, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.Find<T>(condition, order, pageIndex, pageSize, queryOption);
        }

        public virtual List<T> GetBySql<T>(string sql) where T : class, IDataModel
        {
            return DBRepertory.GetBySql<T>(sql);
        }

        public virtual int ExecuteSql(string sql)
        {
            return DBRepertory.ExecuteSql(sql);
        }

        public virtual object ExecuteScalar(string sql)
        {
            return DBRepertory.ExecuteScalar(sql);
        }


        public virtual bool Add<T>(T entity, bool saveNow = true) where T : class, IDataModel
        {
            return DBRepertory.Add<T>(entity, saveNow);
        }

        public virtual bool Update<T>(T entity, bool saveNow = true) where T : class, IDataModel
        {
            return DBRepertory.Update<T>(entity, saveNow);
        }

        public virtual bool Delete<T>(Guid key, bool saveNow = true) where T : class, IDataModel
        {
            return DBRepertory.Delete<T>(key, saveNow);
        }

        public virtual T GetByID<T>(Guid key, EnumQueryOption queryOption = EnumQueryOption.实时) where T : class, IDataModel
        {
            return DBRepertory.GetByID<T>(key, queryOption);
        }

        public bool Add<T>(List<T> entityList, bool saveNow = true) where T : class, IDataModel
        {
            return DBRepertory.Add<T>(entityList, saveNow);
        }

        public bool Update<T>(List<T> entityList, bool saveNow = true) where T : class, IDataModel
        {
            return DBRepertory.Update<T>(entityList, saveNow);
        }

        public bool Delete<T>(List<T> entityList, bool saveNow = true) where T : class, IDataModel
        {
            return DBRepertory.Delete<T>(entityList, saveNow);
        }

        public bool Delete<T>(List<Guid> keyList, bool saveNow = true) where T : class, IDataModel
        {
            return DBRepertory.Delete<T>(keyList, saveNow);
        }
    }
}
